<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-14
 * Time: 上午10:42
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Question\Model;


use Think\Model;

class QuestionAnswerModel extends Model{
    protected $tableName='question_answer';

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_UPDATE),
        array('status', '1', self::MODEL_INSERT),
    );

    protected $insertField='uid,question_id,content,support,oppose,answer_num,to_answer_id,to_response_id';//新增数据时允许操作的字段
    protected $updateField='uid,question_id,content,support,oppose,answer_num,to_answer_id,to_response_id';//新增数据时允许操作的字段

    public function createAnswer($data=array()){
        $data=$this->create($data);
        if (!$data) return false;
        $result=$this->add($data);
        if($result){
            S('answer_question_list_'.is_login(),null);
            if($data['to_answer_id']){
                S('question_answer_with_question_'.$data['to_answer_id'],null);
                S('question_answer_'.$data['to_answer_id'],null);
            }
        }
        return $result;
    }

    /**
     * @param array $map
     * @param int $limit 获取个数 0:获取全部
     * @param $totalCount
     * @param string $order
     * @return array
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getAnswerList($map=array(),$limit=0,&$totalCount,$order='id desc')
    {
        $map=array_merge($map,array('status'=>1,'to_answer_id'=>0));
        if($limit){
            $listIds=$this->where($map)->field('id')->limit($limit)->order($order)->select();
        }else{
            $listIds=$this->where($map)->field('id')->order($order)->select();
        }
        $listIds=array_column($listIds,'id');
        $totalCount=$this->where($map)->count();
        $list=array();
        foreach($listIds as $v){
            $list[]=$this->getAnswer($v);
        }
        unset($v);
        return $list;
    }

    public function getResponseList($answer_id,&$totalCount,$limit=0,$order='id desc')
    {
        $map=array('status'=>1,'to_answer_id'=>$answer_id);
        if($limit){
            $listIds=$this->where($map)->field('id')->limit($limit)->order($order)->select();
        }else{
            $listIds=$this->where($map)->field('id')->order($order)->select();
        }
        $listIds=array_column($listIds,'id');
        $totalCount=$this->where($map)->count();
        $list=array();
        foreach($listIds as $v){
            $list[]=$this->getAnswer($v);
        }
        unset($v);
        return $list;
    }

    public function getAnswer($answer_id=0){
        $data=S('question_answer_'.$answer_id);
        if(!$data){
            $data=$this->where(array('id'=>$answer_id,'status'=>1))->find();
            $data['user']=query_user(array('uid','nickname','space_url','avatar64','address','doing'),$data['uid']);
            if($data['to_response_id']){
                $data['to_response_uid']=$this->where(array('id'=>$data['to_response_id']))->field('uid')->find();
                $data['to_response_user']=query_user(array('uid','nickname','space_url'),$data['to_response_uid']['uid']);
            }else if($data['to_answer_id']){
                $data['to_response_uid']=$this->where(array('id'=>$data['to_answer_id']))->field('uid')->find();
                $data['to_response_user']=query_user(array('uid','nickname','space_url'),$data['to_response_uid']['uid']);
            }
            S('question_answer_'.$answer_id,$data,60);
        }
        return $data;
    }

    public function addFieldNum($ids=array(),$field='answer_num'){
        if(count($ids)>0){
            $this->where(array('id'=>array('in',$ids)))->setInc($field);
            foreach($ids as $v){
                S('question_answer_'.$v,null);
            }
            unset($v);
        }
        return;
    }

    public function getAnswerWithQuestion($answer_id=0){
        $data=S('question_answer_with_question_'.$answer_id);
        if(!$data){
            $data=$this->where(array('id'=>$answer_id,'status'=>1))->find();
            $data['user']=query_user(array('uid','nickname','space_url','avatar64','address','doing'),$data['uid']);
            if (mb_strlen(op_t($data['content']), 'utf-8') < 100) {
                $data['content_s'] = op_t($data['content']);
                $data['have_more'] = false;
            } else {
                $data['content_s'] = msubstr(op_t($data['content']), 0, 100);
                $data['have_more'] = true;
            }
            $data['content']=parse_expression($data['content']);
            $data['content_s']=parse_expression($data['content_s']);
            $data['question']=D('Question/Question')->getQuestion($data['question_id']);
            S('question_answer_with_question_'.$answer_id,$data,60);
        }
        return $data;
    }
    public function answeredQuestion($uid=0,$page=1,&$totalCount,$r=50){
        $uid=$uid?$uid:is_login();
        $answerIds=S('answer_question_list_'.$uid);
        if(!$answerIds){
            $map['uid']=$uid;
            $map['status']=1;
            $map['to_answer_id']=0;
            $answerIds=$this->where($map)->field('id')->order('create_time desc')->select();
            $answerIds=array_column($answerIds,'id');
            S('answer_question_list_'.$uid,$answerIds,60);
        }
        $totalCount=count($answerIds);
        $answerIds=array_slice($answerIds,($page-1)*$r,$r);
        $answer_list=array();
        foreach($answerIds as $val){
            $answer_list[]=$this->getAnswerWithQuestion($val);
        }
        unset($val);
        return $answer_list;
    }

    public function answeredQuestionUids($uids=array(),$r=100){
        $answer_list=S('answer_question_list_friends_'.is_login());
        if(!$answer_list){
            $map['uid']=array('in',$uids);
            $map['status']=1;
            $map['to_answer_id']=0;
            $answerIds=$this->where($map)->field('id')->order('create_time desc')->limit($r)->select();
            $answerIds=array_column($answerIds,'id');
            $answer_list=array();
            foreach($answerIds as $val){
                $answer_list[]=$this->getAnswerWithQuestion($val);
            }
            unset($val);
            S('answer_question_list_friends_'.is_login(),$answer_list,60);
        }
        return $answer_list;
    }

    public function getUidById($id){
        $uid=$this->field('uid')->find($id);
        return $uid['uid'];
    }
} 